﻿using QG.JobBack.BLL.IService;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.JobBack.BLL.Service
{
    public class PictureService :  BaseRepository<PictureInfo>,IPictureService
    {
        

        public List<PictureInfoBase> QueryPictureAll(string PicName, Nullable<bool> Status, Nullable<DateTime> CreateTime)
        {
            using (var db = new qgpingEntities())
            {
                var str = from a in db.Pictureinfo
                          join b in db.Typeinfo
                          on a.TypeId equals b.TypeId
                          select new PictureInfoBase()
                          {
                              PicId = a.PicId,
                              PicName = a.PicName,
                              Src = a.Src,
                              Remark = a.Remark,
                              TypeId = a.TypeId,
                              TypeName=b.TypeName,
                              Status =a.Status,
                              CreateTime=a.CreateTime
                          };
                var tolists = str.ToList();

                if (string.IsNullOrEmpty(PicName))
                {
                    return tolists.ToList<PictureInfoBase>();
                }
                if (!string.IsNullOrEmpty(PicName))
                {
                    tolists = tolists.Where(a => a.PicName == PicName).ToList();
                }
                if (Status==null)
                {
                    return tolists.ToList<PictureInfoBase>();
                }
                if(Status==true)
                {
                    tolists = tolists.Where(a => a.Status == true).ToList();
                }
                else if(Status == false)
                {
                    tolists = tolists.Where(a => a.Status == false).ToList();
                }
                if (!string.IsNullOrEmpty(CreateTime.ToString()))
                {
                    tolists = tolists.Where(a => a.CreateTime.ToString("yyyy-MM-dd") == CreateTime.Value.ToString("yyyy-MM-dd")).ToList();
                }

                return tolists.ToList<PictureInfoBase>();
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool UpdatePictureStar(PictureInfo pictureInfo)
        {
            using (var db = new qgpingEntities())
            {
                PictureInfo pictureInfos = db.Pictureinfo.FirstOrDefault(a => a.PicId == pictureInfo.PicId);
                pictureInfos.Status = pictureInfo.Status;
                return db.SaveChanges() > 0;
            }
        }
    }
}
